const commentService = require("../service/comment.service");
const { to } = require("../utils");
const { UNKNOWN_ERROR } = require("../config/error-constants");
class CommentController {
    async add(ctx, next) {
        const { id } = ctx.user;
        const { content, moment_id } = ctx.request.body;
        const [error, resp] = await to(commentService.add(content, moment_id, id));
        if (error) {
            return ctx.app.emit("error", UNKNOWN_ERROR, ctx);
        }

        ctx.body = {
            code: 0,
            message: "创建评论成功~",
        };
    }

    async reply(ctx, next) {
        const { id } = ctx.user;
        const { content, moment_id, comment_id } = ctx.request.body;
        const [error, resp] = await to(commentService.reply(content, moment_id, comment_id, id));
        if (error) {
            return ctx.app.emit("error", UNKNOWN_ERROR, ctx);
        }

        ctx.body = {
            code: 0,
            message: "回复评论成功~",
        };
    }

    async list(ctx, next) {
        const { moment_id, pageSize, pageNo } = ctx.request.body;
        const [error, resp] = await to(commentService.list(moment_id, pageSize, pageNo));
        const { list, total } = resp || {};
        if (!error) {
            ctx.body = {
                code: 0,
                data: {
                    list,
                    pageSize,
                    pageNo,
                    total,
                },
            };
        }
    }

    async remove(ctx, next) {
        const { commentId } = ctx.params;
        const [error, resp] = await to(commentService.remove(commentId));
        if (error) {
            return ctx.app.emit("error", UNKNOWN_ERROR, ctx);
        }
        ctx.body = {
            code: 0,
            message: "删除评论成功~",
        };
    }
}

module.exports = new CommentController();
